﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.UI;
using RanchBuddy.Core.Providers.Impl;
using RanchBuddy.Core.Services.Impl;

namespace RanchBuddy.Web.Services
{
    public static class Extensions
    {
        public static string GetControllerName(this Type controllerType)
        {
            return controllerType.Name.Replace("Controller", string.Empty);
        }

        public static string GetActionName(this LambdaExpression actionExpression)
        {
            return ((MethodCallExpression)actionExpression.Body).Method.Name;
        }

        public static MvcSitemapProvider.MvcSiteMapNode ToMvcNode(this SiteMapNode node)
        {
            if (SiteMap.RootNode == node)
                return null;
            
            return (MvcSitemapProvider.MvcSiteMapNode)node;
        }

        public static List<MvcSitemapProvider.MvcSiteMapNode> ToMvcNodeList(this SiteMapNodeCollection nodes)
        {
            List<MvcSitemapProvider.MvcSiteMapNode> result = new List<MvcSitemapProvider.MvcSiteMapNode>();
            foreach (SiteMapNode node in nodes)
            {
                result.Add(node.ToMvcNode());
            }
            return result;
        }

        public static string ActionLinkEx(this HtmlHelper helper, string Action, string Controller, string Label)
        {
            string result = "<a class=\"primarynavlink\" href=\"" + new UrlHelper(helper.ViewContext.RequestContext).Action(Action, Controller) + "\">" + Label + "</a>";
            return result;
        }

        public static string ActionLinkEx(this HtmlHelper helper, string Action, string Controller, string Label, object HtmlAttributes)
        {
            string result = "<a class=\"primarynavlink\" href=\"" + new UrlHelper(helper.ViewContext.RequestContext).Action(Action, Controller) + "\">" + Label + "</a>";
            return result;
        }

        public static string ActionLinkEx(this HtmlHelper helper, string Action, string Controller, string Label, string ImagePath)
        {
            string result = "<a class=\"primarynavlink\" href=\"" + new UrlHelper(helper.ViewContext.RequestContext).Action(Action, Controller) + "\"><img src=\"" + ImagePath + "\" alt=\"" + Label + "\" /></a>";
            return result;
        }

        public static string TextBoxEx(this HtmlHelper h, string Label, string ID)
        {
            string result = "<p><label for=\"" + ID + "\">" + Label + ":</label>" + h.TextBox(ID) + h.ValidationMessage(ID) + "</p>";
            return result;
        }

        public static string TextBoxExWithHelp(this HtmlHelper h, string Label, string ID, Page p, int HelpMessageID, int RandomNumber)
        {
            string result = "<p><label for=\"" + ID + "\">" + Label + ":</label>" + h.TextBox(ID) + " " + h.Help(p, HelpMessageID,RandomNumber) + " " + h.ValidationMessage(ID) + "</p>";
            return result;
        }

        public static string PasswordEx(this HtmlHelper h, string Label, string ID)
        {
            string result = "<p><label for=\"" + ID + "\">" + Label + ":</label>" + h.Password(ID) + h.ValidationMessage(ID) + "</p>";
            return result;
        }

        /// <summary>
        /// This method generates a help icon that controls a jquery 
        /// based popup that performs a partial load of the help information 
        /// via an ajax call to the _help.ascx user control.
        /// </summary>
        /// <param name="h">The HtmlHelper class that is being extended by this method</param>
        /// <param name="p">The page that contains this output</param>
        /// <param name="HelpMessageID">The help message id that is to be loaded</param>
        /// <param name="n">If there are many help icons on a page, a random number is required so that each icon is unique</param>
        /// <returns></returns>
        public static string Help(this HtmlHelper h, Page p, int HelpMessageID, int n)
        {
            string result = "<a href=\"#\" class=\"jqModal" + n + "\"><img border=\"0\" src=\"/app_themes/" + p.Theme + "/images/icon_question.gif\" /></a>" +
                            @"<script>$().ready(function() {$('#dialog" + n + "').jqm({ ajax: '/home/help?partial=true&helpid=" + HelpMessageID + "', trigger: 'a.jqModal" + n + "' });});</script><div class=\"jqmWindow\" id=\"dialog" + n + "\">Loading...<img src=\"/app_themes/" + p.Theme + "/images/loading.gif\" /></div>";
            return result;
        }

        public static string TalkingHorse(this HtmlHelper h, Page p, string Message)
        {
            string result =
                @"
           <div style=""margin-right:10%;margin-left:10%;"">
                <table>
                    <tr>
                        <td>
                            <img src=""" + ConfigurationService.GetRoot() + @"App_Themes/" + p.Theme + @"/images/horse.jpg"" />
                            <img style=""z-index:1;position:relative;top:-120px;left:24px"" src=""" + ConfigurationService.GetRoot() + @"App_Themes/" + p.Theme + @"/images/message_triangle.jpg"" />
                        </td>
                        <td>
                            <div><img src=""" + ConfigurationService.GetRoot() + @"App_Themes/" + p.Theme + @"/images/message_top.jpg"" /></div>
                            <div style=""background:solid thin #ff0000;display:block;position:relative;background-position:right;width:300px;background-repeat:repeat-y;background-image:url('" + ConfigurationService.GetRoot() + "App_Themes/" + p.Theme + @"/images/message_bg.jpg');""><div style=""margin-left:30px;margin-right:10px;"">" + Message + @"</div></div>
                            <div><img src=""" + ConfigurationService.GetRoot() + @"App_Themes/" + p.Theme + @"/images/message_bottom.jpg"" /></div>
                        </td>
                    </tr>
                </table>
           </div>";

            return result;
        }
    }
}
